﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Common;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;

namespace ServerMonitoringAssistantWF
{
    public partial class FrmSiteEdit : Form
    {
        public int RecordId { get; set; }
        public FrmSiteEdit()
        {
            InitializeComponent();
        }

        private void FrmSiteEdit_Load(object sender, EventArgs e)
        {
            cbb_status.SelectedItem = "是";
            if (RecordId > 0)
            {
                string sqlstmt = "select * from t_sites where id=@id";
                DbParameter[] dbparams = new DbParameter[]
                {
                            SqliteMgr.me.CreateParameterInputInteger("@id",RecordId)
                };
                DataTable dtRecord = SqliteMgr.me.ExecuteDataTable(sqlstmt, dbparams);
                if (dtRecord.Rows.Count > 0)
                {
                    tb_title.Text = dtRecord.Rows[0]["title"].ToString();
                    tb_url.Text = dtRecord.Rows[0]["url"].ToString();
                    tb_remark.Text = dtRecord.Rows[0]["remark"].ToString();
                    tb_sort.Value = int.Parse(dtRecord.Rows[0]["sort"].ToString());
                    cbb_status.SelectedItem = dtRecord.Rows[0]["status"].ToString() == "1" ? "是" : "否";
                }

            }
        }

        private void btn_save_Click(object sender, EventArgs e)
        {
            string url = tb_url.Text;
            string title = tb_title.Text;
            string status = null == cbb_status.SelectedItem ? "是" : cbb_status.SelectedItem.ToString();
            if (url.Length < 1 || title.Length < 1)
            {
                MessageBox.Show("表单内容无效!", "错误");
                return;
            }
            if (url.IndexOf("://") < 0)
            {
                url = "http://" + url;
            }
            List<DbParameter> dbparams = new List<DbParameter>();
            dbparams.Add(SqliteMgr.me.CreateParameterInputString("@title", title));
            dbparams.Add(SqliteMgr.me.CreateParameterInputString("@url", url));
            dbparams.Add(SqliteMgr.me.CreateParameterInputString("@remark", tb_remark.Text));
            dbparams.Add(SqliteMgr.me.CreateParameterInputInteger("@sort", (int)tb_sort.Value));
            dbparams.Add(SqliteMgr.me.CreateParameterInputInteger("@status", "是"==status?1:0));
            string sqlstmt;
            if (RecordId > 0)
            {
                sqlstmt = "update t_sites set title=@title, url=@url, remark=@remark, sort=@sort, status=@status where id=@id";
                dbparams.Add(SqliteMgr.me.CreateParameterInputInteger("@id", RecordId));
            }
            else
            {
                sqlstmt = "insert into t_sites (title, url, remark, sort, status) values (@title, @url, @remark, @sort, @status)";
            }
            try
            {
                int count = SqliteMgr.me.ExecuteNoneQuery(sqlstmt, dbparams.ToArray());
                if (count > 0)
                {
                    MessageBox.Show("保存成功", "结果");
                    if (null != this.Owner)
                    {
                        (this.Owner as FrmSites).LoadMain();
                    }
                    this.Close();
                }
                else
                {
                    MessageBox.Show("保存失败", "结果");
                }
            }
            catch (DbException ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
